package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.workdetail;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter;

/**
 * @author zhangyu
 * @date 2019/4/16
 * 代理处理ui逻辑
 */
public class UiDelegate {
    private final TextView mTvAddWork;
    private final TextView mTvDiagnose;
    private final RecyclerView mRecyclerViewDiagnose;
    private final DiagnoseAdapter mDiagnoseAdapter;
    private final RecyclerView mRecyclerViewProgram;
    private final TroubleProgramAdapter mProgramAdapter;
    private final TextView mTvButton;
    private final Context mContext;
    public STATE mSTATE = STATE.CONFIRM_DIAGNOSE;

    public enum STATE {
        CONFIRM_DIAGNOSE,
        CONFIRM_PROGRAM
    }

    public UiDelegate(TextView tvAddWork,
                      TextView tvDiagnose, RecyclerView recyclerViewDiagnose,
                      DiagnoseAdapter diagnoseAdapter,
                      RecyclerView recyclerViewProgram,
                      TroubleProgramAdapter programAdapter,
                      TextView tvButton, Context context) {
        mTvAddWork = tvAddWork;
        mTvDiagnose = tvDiagnose;
        mRecyclerViewDiagnose = recyclerViewDiagnose;
        mDiagnoseAdapter = diagnoseAdapter;
        mRecyclerViewProgram = recyclerViewProgram;
        mProgramAdapter = programAdapter;
        mTvButton = tvButton;
        mContext = context;
    }

    public void initState(int state) {
        if (state < 3) {
            mTvAddWork.setVisibility(View.VISIBLE);
            mTvButton.setVisibility(View.VISIBLE);
        } else {
            mTvAddWork.setVisibility(View.INVISIBLE);
            mTvButton.setVisibility(View.INVISIBLE);
        }
    }

    public void changeStateWithData() {
        if (mDiagnoseAdapter != null) {
            List<DiagnoseBean> all = mDiagnoseAdapter.getData();
            if (all.isEmpty()) {
                mTvDiagnose.setVisibility(View.INVISIBLE);
                mRecyclerViewDiagnose.setVisibility(View.INVISIBLE);
                mRecyclerViewProgram.setVisibility(View.INVISIBLE);
                mTvButton.setVisibility(View.INVISIBLE);
            } else {
                mTvDiagnose.setVisibility(View.VISIBLE);
                mRecyclerViewDiagnose.setVisibility(View.VISIBLE);
                mTvButton.setVisibility(View.VISIBLE);
                if (mDiagnoseAdapter.getTempList().isEmpty()) {
                    //没有临时诊断，只有确认的诊断
                    if (mProgramAdapter.getData().isEmpty()) {
                        //没有方案
                        mRecyclerViewProgram.setVisibility(View.INVISIBLE);
                        mTvButton.setText("诊断已确认");
                        mTvButton.setBackground(mContext.getResources()
                                .getDrawable(R.drawable.shape_btn_operation_t));
                        mTvButton.setTextColor(Color.parseColor("#ffffff"));
                        mTvButton.setEnabled(false);
                    } else {
                        //有方案
                        mRecyclerViewProgram.setVisibility(View.VISIBLE);
                        mTvButton.setText("确认方案");
                        mSTATE = STATE.CONFIRM_PROGRAM;
                        mTvButton.setBackground(mContext.getResources()
                                .getDrawable(R.drawable.selector_btn_operation));
                        mTvButton.setEnabled(true);
                        mTvButton.setTextColor(Color.parseColor("#333333"));
                    }
                } else {
                    //有临时诊断,不管有无方案都是先确认诊断
                    if (mProgramAdapter.getData().isEmpty()) {
                        //没有方案
                        mRecyclerViewProgram.setVisibility(View.INVISIBLE);
                    } else {
                        mRecyclerViewProgram.setVisibility(View.VISIBLE);
                    }
                    mTvButton.setText("确认诊断");
                    mSTATE = STATE.CONFIRM_DIAGNOSE;
                    mTvButton.setBackground(mContext.getResources()
                            .getDrawable(R.drawable.selector_btn_operation));
                    mTvButton.setEnabled(true);
                    mTvButton.setTextColor(Color.parseColor("#333333"));
                }
            }
        }

    }

    public STATE getSTATE() {
        return mSTATE;
    }
}
